package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;


import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.StoreOptRecordMapper;
import com.zhijian.warehouse.manage.entity.dto.stock.StockActionDto;
import com.zhijian.warehouse.manage.entity.dto.stock.StockStorageDto;
import com.zhijian.warehouse.manage.entity.export.StoreOptDetRecordExport;
import com.zhijian.warehouse.manage.entity.export.StoreOptRecordExport;
import com.zhijian.warehouse.manage.entity.po.StoreOptRecord;
import com.zhijian.warehouse.manage.entity.vo.request.StoreOptRecordSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreOptRecordSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreOutRecordSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreOptRecordVo;
import com.zhijian.warehouse.manage.enums.StockActionEnum;
import com.zhijian.warehouse.manage.service.StoreOptRecordService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 影响库存操作记录表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-28 10:28:44
 */
@Slf4j
@Service
public class StoreOptRecordServiceImpl implements StoreOptRecordService {

    @Autowired
    private StoreOptRecordMapper mapper;

    @Override
    public StoreOptRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreOptRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreOptRecord storeOptRecord) {
        Date now = new Date();
        storeOptRecord.setCreateTime(now);
        return mapper.insert(storeOptRecord) > 0 ? storeOptRecord.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreOptRecord storeOptRecord) {
        Date now = new Date();
        storeOptRecord.setCreateTime(now);
        return mapper.insertSelective(storeOptRecord) > 0 ? storeOptRecord.getId() : 0L;
    }

    @Override
    public boolean update(StoreOptRecord storeOptRecord) {
        return mapper.updateByPrimaryKey(storeOptRecord) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreOptRecord record) {
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreOptRecordSaveOrUpdateVo saveOrUpdateVo) {
        StoreOptRecord storeOptRecord = new StoreOptRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, storeOptRecord);
        return this.insert(storeOptRecord) != 0;
    }

    @Override
    public boolean modify(StoreOptRecordSaveOrUpdateVo saveOrUpdateVo) {
        StoreOptRecord old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreOptRecord storeOptRecord = new StoreOptRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, storeOptRecord);
        return this.update(storeOptRecord);
    }

    @Override
    public StoreOptRecordVo selectOne(StoreOptRecordSearchVo storeOptRecordSearchVo) {
        return mapper.selectOne(storeOptRecordSearchVo);
    }

    @Override
    public Integer count(StoreOptRecordSearchVo storeOptRecordSearchVo) {
        return mapper.count(storeOptRecordSearchVo);
    }

    @Override
    public List<StoreOptRecordVo> selectList(StoreOptRecordSearchVo storeOptRecordSearchVo) {
        return mapper.selectList(storeOptRecordSearchVo);
    }

    /**
     *
     * {@inheritDoc}
     */
    @Override
    public List<StoreOptRecordExport> selectStoreOptExportList(StoreOptRecordSearchVo param) {
        return mapper.selectStoreOptExportList(param);
    }

    @Override
    public List<StoreOptDetRecordExport> storeOptDetailExport(StoreOptRecordSearchVo param) {
        return mapper.storeOptDetailExport(param);
    }

    @Override
    public Page<StoreOptRecordVo> selectPage(StoreOptRecordSearchVo storeOptRecordSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeOptRecordSearchVo.getPageNum()) || Objects.isNull(storeOptRecordSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeOptRecordSearchVo);
        //分页
        Page<StoreOptRecordVo> page = new Page<>(true, count, storeOptRecordSearchVo.getPageNum(), storeOptRecordSearchVo.getPageSize());
        storeOptRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreOptRecordVo> responseVos = this.selectList(storeOptRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreOptRecord> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }
    /**
     *
     * {@inheritDoc}
     */
    @Override
    @Transactional
    public boolean insertByStockActionDto(StockActionDto stockActionDto) {
        List<StockStorageDto> stockStorages = stockActionDto.getStockStorages();
        StockActionEnum.ACTION_TYPE actionType = stockActionDto.getActionType();
        if (BeanUtil.isEmpty(stockStorages) || BeanUtil.isEmpty(actionType))
            return false;
        List<StoreOptRecord> stockRecords = Collections.synchronizedList(new ArrayList<>());
        Date date = new Date();
        stockStorages.parallelStream().forEach(p -> {
            stockRecords.add(StoreOptRecord.builder()
                    .productId(p.getProductId())
                    .optId(stockActionDto.getOptId())
                    .optType(actionType.value)
                    .optCount(p.getCount().multiply(BigDecimal.valueOf(actionType.genre)))
                    .count(p.getCount())
                    .warehouseId(p.getWarehouseId())
                    .warehouseLocationId(p.getWarehouseLocationId())
                    .voucherCode(stockActionDto.getVoucherCode())
                    .voucherTime(stockActionDto.getVoucherTime())
                    .optUserId(stockActionDto.getOptUserId())
                    .crtUserId(stockActionDto.getCrtUserId())
                    .type(actionType.genre)
                    .createTime(date)
                    .optSource(stockActionDto.getOptSource())
                    .build());
        });
        insertBatch(stockRecords);
        return true;
    }
}
